﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Drawing;

public partial class QuanLyGianHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["UserOnline"] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                ThanhVienDTO  thanhvien = new ThanhVienDTO();
                thanhvien = ThanhVienBUS.LayThanhVienTheoUserName(Session["UserOnline"].ToString());
                if(thanhvien.MaNhomThanhVien == 2)
                    LoadDuLieu();
                else
                    Response.Redirect("~/Default.aspx");
            }            
        }
    }

    private void LoadDuLieu()
    {
        List<GianHangDTO> listGH = GianHangBUS.LayDanhSachGianHang();

        CollectionPager3.PageSize = 30;
        CollectionPager3.DataSource = listGH;
        CollectionPager3.BindToControl = dtlGianHang;
        dtlGianHang.DataSource = CollectionPager3.DataSourcePaged;
    }

    protected void dtlGianHang_ItemCommand(object source, DataListCommandEventArgs e)
    {
        Label ID = (Label)dtlGianHang.Items[e.Item.ItemIndex].FindControl("lbID");
        ImageButton imbEdit = (ImageButton)dtlGianHang.Items[e.Item.ItemIndex].FindControl("imbEdit");
        TextBox tenGianHang = (TextBox)dtlGianHang.Items[e.Item.ItemIndex].FindControl("txtTenGianHang");
        TextBox DiaChi = (TextBox)dtlGianHang.Items[e.Item.ItemIndex].FindControl("txtDiaChi");
        TextBox SoDienThoai = (TextBox)dtlGianHang.Items[e.Item.ItemIndex].FindControl("txtSoDienThoai");
        Label TinhTrang = (Label)dtlGianHang.Items[e.Item.ItemIndex].FindControl("lbTinhTrang");
        DropDownList ddlTinhTrang = (DropDownList)dtlGianHang.Items[e.Item.ItemIndex].FindControl("ddlTinhTrang");
        if (e.CommandName == "edit")
        {
            
            if (tenGianHang.ReadOnly == true)
            {
                ddlTinhTrang.Visible = true;
                tenGianHang.ReadOnly = false;
                DiaChi.ReadOnly = false;
                SoDienThoai.ReadOnly = false;

                tenGianHang.BackColor = Color.White;
                DiaChi.BackColor = Color.White;
                SoDienThoai.BackColor = Color.White;
                imbEdit.ImageUrl = "~/Images/update.png";
            }
            else
            {
                TinhTrang.Text = ddlTinhTrang.Text;
                ddlTinhTrang.Visible = false;
                tenGianHang.ReadOnly = true;
                DiaChi.ReadOnly = true;
                SoDienThoai.ReadOnly = true;
                tenGianHang.BackColor = Color.DarkGray;
                DiaChi.BackColor = Color.DarkGray;
                SoDienThoai.BackColor = Color.DarkGray;
                imbEdit.ImageUrl = "~/Images/edit.png";
                GianHangDTO gianhang = GianHangBUS.LayGianHangTheoID(int.Parse(ID.Text));
                gianhang.TenGianHang = tenGianHang.Text;
                gianhang.DiaChi = DiaChi.Text;
                gianhang.SoDienThoai = SoDienThoai.Text;
                gianhang.TinhTrang = bool.Parse(TinhTrang.Text);
                GianHangBUS.CapNhatGianHang(gianhang);
            }
        }

        if(e.CommandName == "delete")
        {
            int magianhang = int.Parse(ID.Text);
            GianHangBUS.XoaGianHangTheoID(magianhang);
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }

   
}
